package com.hpeu_01.day0111;

public class CheckAccount extends Account{
    //可透支限额
    private double overdraft;

    public double getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }
    //全参构造，用于初始化数据
    public CheckAccount(int id, double balance, double annualInterRate, double overdraft) {
        super(id, balance, annualInterRate);
        this.overdraft = overdraft;
    }
    //重写withdraw方法
    /*
    如果（取款金额<账户余额），可直接取款；
    如果（取款金额>账户余额），计算需要透支的额度，
    判断透支额overdraft是否足够支付本次透支需要，
    如果可以，将账户余额修改为0，重减可透支金额，
    如果不可以，提示用户超过可透支额的限额
     */
    @Override
    public void withdraw(double withdrawal) {
        //如果（取款金额<账户余额）
        if (withdrawal <= getBalance()){
            //更新账户余额
            setBalance(getBalance()-withdrawal);
            System.out.println("取款成功，取款金额为："+withdrawal+",账户余额为:"+getBalance()+"，可透支额度为："+getOverdraft());
        }else if(withdrawal <= (getBalance()+getOverdraft())){
            //更新可透支额度
            setOverdraft((getBalance()+getOverdraft())-withdrawal);
            //更行账户余额
            setBalance(0);
            System.out.println("取款成功，取款金额为："+withdrawal+",账户余额为:"+getBalance()+"，可透支额度为："+getOverdraft());
        }else {
            System.out.println("取款失败，超过可透支额的限额");
        }
    }
}
